package com.iye.puretennis.helper;

import com.iye.puretennis.utils.StringUtils;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Author: Chuanbao on 2014/11/7.
 */
public class ServerAccessHelper {

    private static HttpParams getHttpParams(){
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams,
                30000);
        HttpConnectionParams.setSoTimeout(httpParams,
                30000);
        return httpParams;
    }

    // 获得Get请求对象request
    private static HttpGet getHttpGet(String url,String token) {
        HttpGet get = new HttpGet(url);
        //GOT access token
        get.addHeader("Access_Token", token);
        get.addHeader("Content-Type", "application/json");
        get.addHeader("User-Agent", "imgfornote");
        return get;
    }

    // 获得Post请求对象request
    private static HttpPost getHttpPost(String url,String token) {
        HttpPost post = new HttpPost(url);
        //GOT access token
        post.addHeader("Access_Token", token);
        post.addHeader("Content-Type", "application/json");
        post.addHeader("User-Agent", "imgfornote");
        return post;
    }

    // 根据请求获得响应对象response
    private static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException {
        return new DefaultHttpClient(getHttpParams()).execute(request);
    }

    // 根据请求获得响应对象response
    private static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException {
        return new DefaultHttpClient(getHttpParams()).execute(request);
    }

    // 发送Post请求，获得响应查询结果
    public static String queryStringForPost(String url,String token,StringEntity paramEntity) {
        // 根据url获得HttpPost对象
        HttpPost request = getHttpPost(url,token);
        String result = null;
        try {
            if(paramEntity != null){
                request.setEntity(paramEntity);
            }
            // 获得响应对象
            HttpResponse response = getHttpResponse(request);
            // 判断是否请求成功
            if (response.getStatusLine().getStatusCode() == 200) {
                // 获得响应
                result = EntityUtils.toString(response.getEntity());
                return result;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            result = Constant.RESPONSE_BADLY;
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            result = Constant.RESPONSE_BADLY;
            return result;
        }
        return null;
    }

    // 发送Get请求，获得响应查询结果
    public static String queryStringForGet(String url,String token) {
        // 获得HttpGet对象
        HttpGet request = getHttpGet(url,token);
        String result = null;
        try {
            // 获得响应对象
            HttpResponse response = getHttpResponse(request);
            // 判断是否请求成功
            if (response.getStatusLine().getStatusCode() == 200) {
                // 获得响应
                result = EntityUtils.toString(response.getEntity());
                return result;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            result = Constant.RESPONSE_BADLY;
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            result = Constant.RESPONSE_BADLY;
            return result;
        }
        return null;
    }
}
